package com.prp.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.Transaction;

import com.opensymphony.xwork2.ActionContext;
import com.prp.dao.Adminrole;
import com.prp.dao.AdminroleDAO;
import com.prp.dao.AdminroleId;
import com.prp.dao.Role;
import com.prp.dao.RoleDAO;
import com.prp.dao.Userinfo;
import com.prp.dao.UserinfoDAO;
import com.prp.interfaces.IUserManage;
import com.prp.security.Encript;
import com.prp.security.MailService;

public class UserManage implements IUserManage{
	
	public boolean login(String username , String password){
		UserinfoDAO userinfoDAO = new UserinfoDAO();
		
		List tempList = userinfoDAO.findByUsername(username); 
		
		//tempList.add(userinfoDAO.findById(username));
		
		if( tempList.size() == 0){
			return false;
		}
		Userinfo temp = (Userinfo) tempList.get(0);
		if(temp.getPassword().equalsIgnoreCase(password)&&temp.getIsActive()){
			ActionContext.getContext().getSession().put("username", username);
			return true;
		}else {
			return false;
		}
		
	}
	
	public boolean findPassword(String username , String email){
		UserinfoDAO userinfoDAO = new UserinfoDAO();

		List tempList = userinfoDAO.findByUsername(username);

		userinfoDAO.getSession().clear();
		if( tempList.size() == 0){
			return false;
		}
		Userinfo temp = (Userinfo) tempList.get(0);
		if(!temp.getEmail().equals(email)){
			return false;
		}else {
			MailService mailService= new MailService();
			return mailService.sendPassword(email, temp.getPassword());
		}
	}
	
	public boolean registerUser(Userinfo userinfo){
		if(checkIsAvailable(userinfo))
			return false;
		
		
		UserinfoDAO userinfoDAO = new UserinfoDAO();
		userinfo.setIsActive(false);
		userinfo.setIsAdmin(false);
		Transaction tx =  userinfoDAO.getSession().beginTransaction();
		userinfoDAO.save(userinfo);
		
		MailService mailService= new MailService();
		
		boolean reslut;
		reslut = mailService.sendVertificationMail(userinfo.getEmail(),
				userinfo.getUsername(), userinfo.getPassword(), new java.util.Date());
		
		if (reslut){
			tx.commit();
		}else{
			tx.rollback();
		}
		userinfoDAO.getSession().close();
		
		return reslut;
	}
	
	public boolean checkIsAvailable(Userinfo userinfo){
		UserinfoDAO userinfoDAO = new UserinfoDAO();


		List tempList1 = userinfoDAO.findByUsername(userinfo.getUsername());

		userinfoDAO.getSession().clear();

		List tempList2 = userinfoDAO.findByEmail(userinfo.getEmail());

		userinfoDAO.getSession().clear();
		
		System.out.println(tempList1.size());
		
		if(tempList1.size() == 0 && tempList2.size() == 0){
			return false;
		}
		else{
			return true;
		}
	}

	

	public boolean validateUser(String username, String key, String date) {
		
		try{
			UserinfoDAO userinfoDAO = new UserinfoDAO();
			
			List tempList = userinfoDAO.findByUsername(username);
			
			if( tempList.size() == 0){
				return false;
			}
			Userinfo temp = (Userinfo) tempList.get(0);
			String str = temp.getUsername()+temp.getEmail()+date;
			String key2 = Encript.md5(str);
			
			if(!key.equals(key2)){
				return false;
			}else {
				ActionContext.getContext().getSession().put("username", username);
				Transaction tx = userinfoDAO.getSession().beginTransaction();
				temp.setIsActive(true);
				userinfoDAO.attachDirty(temp);
				tx.commit();
				userinfoDAO.getSession().close();
				return true;
			}
		}
		catch(Exception e){
			return false;
		}
	}

	public Userinfo getUser(String username) {
		UserinfoDAO dao = new UserinfoDAO();
		
		
		// TODO Auto-generated method stub
		return dao.findById(username);
	}
	
	public Userinfo getUserByEmail(String email) {
		UserinfoDAO dao = new UserinfoDAO();
		
		
		// TODO Auto-generated method stub
		List list = dao.findByEmail(email);
		
		if(list.size() != 0)
			return (Userinfo) list.get(0);
		else
			return null;
	}

	public boolean changePassword(String username ,String password) {
		UserinfoDAO userinfoDAO = new UserinfoDAO();
		
		List tempList = userinfoDAO.findByUsername(username); 
		
		//tempList.add(userinfoDAO.findById(username));
		
		if( tempList.size() == 0){
			return false;
		}
		Userinfo temp = (Userinfo) tempList.get(0);
		
		temp.setPassword(password);
		Transaction tx = userinfoDAO.getSession().beginTransaction();
		userinfoDAO.attachDirty(temp);
		tx.commit();
		userinfoDAO.getSession().close();
		return true;
		
	}
	
	public String getLoginUserName(){
		String username = (String) ActionContext.getContext().getSession().get("username");
		
		
		return username;
	}

	
	
	
}
